import 'package:async/async.dart';
import 'package:boss/main.dart';
import 'package:boss/models/takeout_order_list_data.dart';
import 'package:boss/models/takeout_summary.dart';
import 'package:boss/ui/widget/calendar/util/date_util.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';

class TakeoutListProvider extends ChangeNotifier {
  DateTime startTime = DateUtils.startOfTheDay(DateTime.now());

  DateTime endTime = DateUtils.startOfTheDay(DateTime.now());

  String get startTimeText {
    if (startTime == null) {
      return null;
    }

    return flustars.DateUtil.formatDate(startTime, format: flustars.DateFormats.y_mo_d);
  }

  String get endTimeText {
    if (endTime == null) {
      return null;
    }

    return flustars.DateUtil.formatDate(endTime, format: flustars.DateFormats.y_mo_d);
  }

  int type;

  TakeoutSummary takeoutSummary;

  List<TakeoutOrderItem> orders = [];

  GlobalKey<LoaderPullRefreshState> loaderPullRefreshKey = GlobalKey<LoaderPullRefreshState>();

  Future<Result<List<TakeoutOrderItem>>> queryTakeoutOrders(int page) async {
    try {
      var res = await api.queryTakeoutOrders(
        startTime: startTimeText,
        endTime: endTimeText,
        page: page,
        type: type,
      );
      if (!res.successful()) {
        return Result.error(res.msg);
      }

      return Result.value(res.data.items);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  Future<Result<TakeoutSummary>> queryTakeoutSummary() async {
    try {
      var res = await api.queryTakeoutSummary(
        startTime: startTimeText,
        endTime: endTimeText,
      );
      if (!res.successful()) {
        return Result.error(res.msg);
      }

      return Result.value(res.data);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  Future<Result<List<TakeoutOrderItem>>> refreshData() async {
    var takeoutSummary = await queryTakeoutSummary();
    var takeoutOrders = await queryTakeoutOrders(1);

    if (takeoutSummary.isError) {
      return Result.error(takeoutSummary.asError.error);
    }

    if (takeoutOrders.isError) {
      return takeoutOrders;
    }

    this.takeoutSummary = takeoutSummary.asValue.value;
    this.orders = takeoutOrders.asValue.value;
    notifyListeners();

    return Result.value(takeoutOrders.asValue.value);
  }

  Future<Result<List<TakeoutOrderItem>>> loadData() async {
    var page = orders.length ~/ 10 + 1;
    var takeoutOrders = await queryTakeoutOrders(page);

    if (takeoutOrders.isError) {
      return takeoutOrders;
    }

    this.orders.addAll(takeoutOrders.asValue.value);
    notifyListeners();

    return Result.value(takeoutOrders.asValue.value);
  }

  changeStatusList(int type) {
    this.type = type;
    loaderPullRefreshKey.currentState.refresh();
  }

  setStartTime(List<DateTime> dateTime) {
    startTime = dateTime[0];
    endTime = dateTime[1];
    notifyListeners();

    if (startTime != null && endTime != null) {
      loaderPullRefreshKey.currentState.refresh();
    }
  }
}
